﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;

namespace cecbClasses.Data_Adapters
{
    public class Func_ProjectTeams
    {


        public Func_ProjectTeams()
        {
            cecbContext = new DSSEntities();
        }

        DSSEntities cecbContext;
        Teams team;
        TeamMembers tmem;
        public bool InsertProjectTeam(String name, List<Members> list, string memId , List<int> specRefList)
        {
            bool IsInserted = false;
            try
            {
                String reference = getNewProjectTeamReference();
                team = new Teams();
                team.team_reference = reference;
                team.team_name = name;
                 
                // team.Team = memId;
                // team.Team_CreatedDate = DateTime.Now;
                team.team_status = true;
                cecbContext.AddToTeams(team);
                cecbContext.SaveChanges();


                int x = 0;
                foreach (Members value in list)
                {
                    tmem = new TeamMembers();
                    tmem.team_reference = reference;
                    tmem.mem_reference = value.mem_reference;
                    tmem.mem_spec = specRefList[x];
                    // String refe = value.mem_reference;
                    // tmem.MembersReference.Value = cecbContext.Members.First(i => i.mem_reference == refe).mem_reference;
                    tmem.teamMem_status = true;
                    cecbContext.AddToTeamMembers(tmem);
                    cecbContext.SaveChanges();
                }

                IsInserted = true;
            }
            catch (Exception)
            {

                throw;
            }


            return IsInserted;
        }

        public String getNewProjectTeamReference()
        {


            int sub;
           

            try
            {
                var idKey = (from c in cecbContext.Teams
                             orderby c.team_id descending
                             select c).First();

                String id = idKey.team_reference.ToString();

                sub = int.Parse(id.Substring(4)) + 1;
            }
            catch (Exception)
            {
                sub = 1;
            }
            String newId = "TEAM" + sub;
            return newId.ToString();

        }

        public List<Members> getTeamLeaders()
        {
            String designation = "Team Leader";
            IQueryable<Members> Query =
                  from c in cecbContext.Members
                  where c.mem_designation == designation
                  select c;

            List<Members> SelectedEmpList = Query.ToList();

            return SelectedEmpList;

        }

        public List<Teams> getProjectTeams()
        {
            IQueryable<Teams> Query =
                  from c in cecbContext.Teams
                  select c;

            List<Teams> SelectedTeamList = Query.ToList();

            return SelectedTeamList;
        }

    }
     
}
